<?php
/*
 * upload.php
 *
 * Copyright (C) 2006 - OS3 srl - http://www.os3.it
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
function upload_move ( $field_name, $dest_name )
{
	if ( move_uploaded_file ($_FILES [ $field_name ]['tmp_name'], $dest_name ) )
	{
		chmod ( $dest_name, 0644 );
		return ( true );
	} else 
		util_display_error ( "Cannot move this file. Wrong upload.", "From: " . $_FILES [ $field_name ]['tmp_name'] ."<br />To: $dest_name " );

	return ( false );
}

function upload_delete ( $field_name )
{
	if ( upload_exists ( $field_name ) == false ) return;

	unlink ( $_FILES [ $field_name ]['tmp_name'] );
}

function upload_tmp_name ( $field_name )
{
	return ( $_FILES [ $field_name ]['tmp_name'] );
}

function upload_exists ( $field_name )
{
	if ( @ $_FILES [ $field_name ]['size'] ) return ( $_FILES [ $field_name ]['name'] );

	return ( false );
}

function upload_type ( $field_name )
{
	if ( @ $_FILES [ $field_name ]['size'] ) 
		return ( $_FILES [ $field_name ]['type'] );

	return ( false );
}

function upload_real_name ( $field_name )
{
	if ( @ $_FILES [ $field_name ]['size'] )
		return ( $_FILES [ $field_name ] ['name'] );


	return ( false );
}

function upload_extension ( $field_name, $fname = false )
{
	if ( $fname == false )
	{
		$name = upload_real_name ( $field_name );
		if ( $name == false ) return ( false );
	} else
		$name = $fname;


	return ( strtolower ( substr ( strrchr ( $name, "." ), 1 ) ) );
}

function upload_create_unique_name ( $field_name, $dir_name, $base, $full = false )
{
	$ext = upload_extension ( $field_name );

	//print "<h1>EXT: $ext </h1>";

	$fname = tempnam ( $dir_name, $base );
	filesys_del ( $fname );
	$fname .= ".$ext";

	if ( $full == false ) $fname = basename ( $fname );

	return ( $fname );
}
?>
